package com.pruebas.physics.common;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Rectangle {
	private float base;
	private float height;
	
	private float initialX;
	private float initialY;
	
	private FloatBuffer floatBuffer;
	
	public Rectangle(float base, float height){
		this(base, height, 0f, 0f);
	}
	
	public Rectangle(float base, float height, float initialX, float initialY){
		this.base= base;
		this.height = height;
		this.initialX = initialX;
		this.initialY = initialY;
		makefloatBuffer();
	}
	
	public void draw(GL10 gl){
		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, floatBuffer);
		gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
	}
	
	private void makefloatBuffer() {
		float halfBase = this.base / 2;
		float halfHeight = this.height / 2;
		float left = this.initialX - halfBase;
		float right = this.initialX + halfBase;
		float top = this.initialY + halfHeight;
		float buttom = this.initialY - halfHeight;
		
		float[] coords = {
			left,	top,
			left,	buttom,
			right,	buttom,
			right,	top
		};
		
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(coords.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		this.floatBuffer = byteBuffer.asFloatBuffer();
		this.floatBuffer.put(coords);
		this.floatBuffer.position(0);
	}
	
	public float getBase() {
		return base;
	}
	
	public float getHeight() {
		return height;
	}
	
	public float getInitialX() {
		return initialX;
	}
	
	public float getInitialY() {
		return initialY;
	}
}
